package cn.stronglink.asset.module.dam.utils.dataJsonUtils.realtime.receive;

import java.util.Date;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
import com.alibaba.fastjson.JSON;
import cn.stronglink.asset.model.SensorSetting;
import cn.stronglink.asset.module.dam.entity.message.MsgRealTimeDatas;

public class IntercepterRealTimeDatasUtil {

	public static boolean doorFlag = false, isDoor1=true;
	private static int sensorType = -1;
	private static long doorTime1 = 0l, doorTime2 = 0l;// 门数据：间隔时间
	public static  MsgRealTimeDatas realDatas, senDatas,doorDatas;
	//sensorTime1 = 0l, sensorTime2 = 0l, //传感器数据：间隔时间
	public static long  doorDiff = 0l, lastDoorTime=0l,lastSenTime=0l;//门数据间隔时间 ，门接收时间/最后时间，  传感器接收最后时间

	// 计算两次时间： 间隔
	public static boolean isDataGood(Message message, long timeDiff) {
		TextMessage tm = (TextMessage) message;
		SensorSetting[] senArr = null;
		String msg="";
		
		// 取出一级消息类DamMessage数据: 判断是否为指定设备的消息
		try {
			msg = tm.getText();
			
			if (("".equals(msg) || "null".equals(msg) || msg == null))
				return false;
			
			realDatas = JSON.parseObject(msg, MsgRealTimeDatas.class);
		} catch (JMSException e) {
			e.printStackTrace();
		}
		
		System.out.println("开始时间时间=======*******========》》》》" + new Date() + "\n\n");
		System.out.println("接收到数据............." + msg);
		senArr = realDatas.getAwsPostdata().getSensorList();
		
		if (senArr == null || realDatas == null)
			return false;

		if (!"damDc".equals(realDatas.getDevType()) || !"realTimeDatas".equals(realDatas.getActioncode()))
			return false;

		realDatas.setActioncode("screenCommand");// 供以后：使用消息对象转发指令时使用
		for (SensorSetting sen : senArr) {
			sensorType = sen.getSensorType();
			break;
		}
		
		//数据分析
		doorFlag = sensorType == 3 ? true : false;
		
		if (!doorFlag ) {// 保存： 温湿度数据
			senDatas=realDatas;
			lastSenTime=System.currentTimeMillis();//跟踪最后接收时间，供存数据库：判断是否过了5s,以定时更新数据
			return true;
			
		}else{
			doorDatas=realDatas;
			if ( isDoor1) {// 初次接收数据时间
				doorTime1 = System.currentTimeMillis();
				isDoor1=false;
				
			} else if(!isDoor1) {
				doorTime2 = System.currentTimeMillis();
				isDoor1=true;
			}
			// 计算时间差
			doorDiff = Math.abs((doorTime1 - doorTime2));
			lastDoorTime=System.currentTimeMillis();//跟踪最后接收时间，供存数据库：判断是否过了5s,以定时更新数据
			return doorDiff > timeDiff;  // 500毫秒
		}
	}
	
}
